// 真空树脂塞孔机
import { ref } from 'vue';
import { Input, Modal, message } from 'ant-design-vue';
import { useAuthStore } from '@/store/modules/auth';
import { isNonNegativeInteger } from '@/utils';

let topFormModel;
let bottomFormModel;
const lotType = ref('searchInput');
const lotids = ref([]);
const moduleNoSearchData = ref(null);
const authStore = useAuthStore();

export default {
  url: '/qms/single/resinConsent/list',
  addUrl: '/qms/single/resinConsent/add',
  editUrl: '/qms/single/resinConsent/update',
  delUrl: '/qms/single/resinConsent/deleteBatch',
  modalGetTableDataUrl: '/qms/single/resinConsent/', // 缺陷数据获取
  editMethod: 'post',
  searchFormList: [
    {
      type: 'input',
      key: 'moduleNo',
      label: '生产型号',
      placeholder: '请输入生产型号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'lotId',
      label: 'LOT号',
      placeholder: '请输入LOT号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4,
      selectOptions: [
        {
          value: '0',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '3',
          label: '审核完成'
        }
      ]
    },

    {
      type: 'dateRangePicker',
      key: 'time',
      label: '日期',
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 6
    }
  ],
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'moduleNo',
      dataIndex: 'moduleNo',
      title: '生产型号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'lotId',
      dataIndex: 'lotId',
      title: 'LOT号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待领班审核', '待修改', '待主管审核', '审核完成'][record.state];
      }
    },
    {
      key: 'modifyTime',
      dataIndex: 'modifyTime',
      title: '录入时间',
      width: 160,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'quantity',
      dataIndex: 'quantity',
      title: '生产数量PNL',
      width: 135,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'screenCheck',
      dataIndex: 'screenCheck',
      title: '网板检查',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'airPlateCheck',
      dataIndex: 'airPlateCheck',
      title: '导气板检查',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'knifePressure',
      dataIndex: 'knifePressure',
      title: '刮刀压力(MPa)',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'knifeSpeed',
      dataIndex: 'knifeSpeed',
      title: '刮刀速度(mm/s)',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'inkType',
      dataIndex: 'inkType',
      title: '油墨型号',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'inkLotNo',
      dataIndex: 'inkLotNo',
      title: '油墨批号',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'inkOpenTime',
      dataIndex: 'inkOpenTime',
      title: '开罐时间',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'useTime',
      dataIndex: 'useTime',
      title: '使用时间',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'inkUsed',
      dataIndex: 'inkUsed',
      title: '使用油墨KG',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'heatingCycles',
      dataIndex: 'heatingCycles',
      title: '树脂回温次数',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'meshCleaning',
      dataIndex: 'meshCleaning',
      title: '网板清洁',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'airGuidePlateCleaning',
      dataIndex: 'airGuidePlateCleaning',
      title: '导气板清洗',
      width: 130,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'shiftType',
      dataIndex: 'shiftType',
      title: '班别',
      width: 120,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['白班', '夜班'][record.shiftType];
      }
    },
    {
      key: 'checkNum1',
      dataIndex: 'checkNum1',
      title: '首件检查',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkNum2',
      dataIndex: 'checkNum2',
      title: '自主检查',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkNum3',
      dataIndex: 'checkNum3',
      title: '末件检查',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'checkResult',
      dataIndex: 'checkResult',
      title: '检查结果',
      width: 90,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'operator',
      dataIndex: 'operator',
      title: '操作员',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '主管审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],
  searchParam: ref({
    prodNo: null,
    lotId: null,
    state: null,
    time: null
  }),
  scroll: { x: 2500, y: 'auto' },
  topFormList: [
    // 用于获取model
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },
    {
      type: 'searchInput',
      key: 'moduleNo',
      label: '生产型号',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      span: 8,
      rule: {
        required: true,
        message: '请输入生产型号',
        trigger: 'change'
      },
      transfromSearch(model, searchData) {
        topFormModel = model;
        moduleNoSearchData.value = searchData;
        Object.keys(model.value).forEach(item => {
          model.value[item] = searchData[item] || model.value[item];
        });
        lotids.value =
          searchData?.lotNo?.map(item => {
            return {
              label: item.lotNo,
              value: item.lotNo
            };
          }) || [];
        if (lotids.value.length) {
          lotType.value = 'select';
          model.value.lotId = null;
        } else {
          lotType.value = 'searchInput';
        }
      }
    },
    {
      type: lotType,
      key: 'lotId',
      label: 'Lot号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      disabled: false,
      rule: {
        required: true,
        message: '请输入Lot号',
        trigger: 'change'
      },
      searchKey: 'lotNumber',
      transfromSearch(model, searchData) {
        topFormModel = model;
        moduleNoSearchData.value = searchData;
        Object.keys(model.value).forEach(item => {
          model.value[item] = searchData[item] || model.value[item];
        });
      },
      selectOptions: lotids
    },
    {
      type: '',
      span: 8
    },
    {
      type: 'datePicker',
      key: 'modifyTime',
      label: '录入时间',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    },

    {
      type: 'input',
      key: 'quantity',
      label: '数量',
      rule: {
        required: true,
        message: '请输入生产数量',
        trigger: 'change'
      },
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: '',
      span: 8
    },

    {
      type: 'radio',
      key: 'screenCheck',
      label: '网板检查',
      span: 8,
      defaultValue: 'OK',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },

    {
      type: 'radio',
      key: 'airPlateCheck',
      label: '导气板检查',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      defaultValue: 'OK',
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ]
    },

    {
      type: 'input',
      key: 'knifePressure',
      label: '刮刀压力(MPa)',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'knifeSpeed',
      label: '刮刀速度(mm/s)',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'inkType',
      label: '油墨型号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      defaultValue: 'TP-2900 '
    },
    {
      type: 'input',
      key: 'inkLotNo',
      label: '油墨批号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'datePicker',
      key: 'inkOpenTime',
      label: '开罐时间',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true,
      change(e) {
        topFormModel.value.useTime = e;
      }
    },

    {
      type: 'datePicker',
      key: 'useTime',
      label: '使用时间',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    },
    {
      type: 'input',
      key: 'inkUsed',
      label: '使用油墨(KG)',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'input',
      key: 'heatingCycles',
      label: '树脂回温次数',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      defaultValue: 1
    },
    {
      type: 'radio',
      key: 'meshCleaning',
      label: '网板清洁',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ],
      defaultValue: 'OK'
    },
    {
      type: 'radio',
      key: 'airGuidePlateCleaning',
      label: '导气板清洗',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: 'OK',
          value: 'OK'
        },
        {
          label: 'NG',
          value: 'NG'
        }
      ],
      defaultValue: 'OK'
    },
    {
      type: 'radio',
      key: 'shiftType',
      label: '班别',
      span: 4,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          label: '白班',
          value: '0'
        },
        {
          label: '夜班',
          value: '1'
        }
      ]
    },
    {
      type: 'select',
      key: 'checkType',
      label: '检查类型',
      span: 6,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          value: '首件检查',
          label: '首件检查'
        },
        {
          value: '自主检查',
          label: '自主检查'
        },
        {
          value: '末件检查',
          label: '末件检查'
        }
      ]
    },
    {
      type: 'input',
      key: 'checkNum',
      label: '检查数量',
      span: 6,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'radio',
      key: 'checkResult',
      label: '检查结果',
      span: 6,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          value: 'OK',
          label: 'OK'
        },
        {
          value: 'NG',
          label: 'NG'
        }
      ]
    },
    {
      type: 'textarea',
      key: 'remark',
      label: '备注',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 24
    }
  ],
  bottomList: [
    {
      type: 'input',
      key: 'operator',
      label: '操作员',
      defaultValue: authStore.userInfo.user.nickName || authStore.userInfo.user.userName,
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'foreman',
      label: '领班审核',
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'examiner',
      label: '主管审核',
      disabled: true,
      span: 8
    }
  ],
  modalHeight: '90vh',
  modalTableKey: 'badId',
  modalAddTableKey: 'badDoList',
  modalTableColumn: [
    {
      key: 'index',
      dataIndex: 'index',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'badName',
      dataIndex: 'badName',
      title: '检验项目',
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'number',
      dataIndex: 'number',
      title: '缺陷数量',
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        if (record.disabled) {
          return <div style="min-width:150px">{record.number}</div>;
        }
        return (
          <Input
            style="height: 22px !important;"
            value={record.number}
            onBlur={e => {
              const value = e.target.value;
              if (!(isNonNegativeInteger(Number(value).toString()) && Number(value >= 0))) {
                message.error('请输入正整数');
                record.number = null;
                return;
              }
              record.number = Number(value);
            }}
            onKeypress={e => {
              console.log('e :>> ', e);
            }}
          ></Input>
        );
      }
    }
  ],
  modleSearchUrlObj: {
    lotId: '/rcerp/agv/erp/findByLotNo',
    moduleNo: '/rcerp/agv/erp/findByModuleNo'
  },
  modalGetTableUrlSuffix: {
    add: 'selectBadId',
    byId: 'listBad'
  },
  addSuccessColse: false, // 新增成功后是否关闭弹窗
  modalClose() {
    lotType.value = 'searchInput';
    moduleNoSearchData.value = null;
    lotids.value = [];
  },

  addSuccess(val) {
    if (val.msg === '累计次数超过1100,请更换砂带') {
      Modal.confirm({
        title: `${val.msg}`,
        onOk: () => {
          message.success('已确定');
        },
        onCancel: () => {
          message.info('操作已取消');
        }
      });
    }
  }
};
